﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using System.Data;
using TTSBLL;
using TTS;
using TTSModel;

public partial class T0002038_Add : System.Web.UI.Page
{
    private Assemble a;
    private string message;
    private  AssembleInfo ainfoanther;
    private UnitType u;
    private AssembleItem assembleItem;
    //private string T0002038_SN = System.Guid.NewGuid().ToString(); //直接返回字符串类型
    private const string kccDownPEC = "KCQR01AA";
    //private IList<UnitTypeInfo> assembleVersionListT0002039;
    private int versionNum_T0002039 = 4;
    private string[] kccDownVersion = new string[] { "01", "02" };
   // private string T0002038_SN { get { return ViewState["T0002038_SN"].ToString(); } set { ViewState["T0002038_SN"] = value; } }

    private string T0002038_TAK_SN { get { return ViewState["T0002038_TAK_SN"].ToString(); } set { ViewState["T0002038_TAK_SN"] = value; } }
    private string T0002038_KccUp_SN { get { return ViewState["T0002038_KccUp_SN"].ToString(); } set { ViewState["T0002038_KccUp_SN"] = value; } }
    private string T0002038_KccDown_SN { get { return ViewState["T0002038_KccDown_SN"].ToString(); } set { ViewState["T0002038_KccDown_SN"] = value; } }
    private string T0002039_SN { get { return ViewState["T0002039_SN"].ToString(); } set { ViewState["T0002039_SN"] = value; } }
    private string T0002040_SN { get { return ViewState["T0002040_SN"].ToString(); } set { ViewState["T0002040_SN"] = value; } }

    private DataRow dr;
    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }
     public T0002038_Add()
    {
        a = new Assemble();
        ainfoanther = new AssembleInfo();
        u = new UnitType();
        assembleItem = new AssembleItem();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
           // assembleVersionListT0002039 = u.LoadEntities("T0002039");
           // this.T0002039_Version.Text = assembleVersionListT0002039[0].TcodeVersion + "," + assembleVersionListT0002039[1].TcodeVersion;
            this.T0002038_Version.Text = kccDownVersion[0] + "," + kccDownVersion[1];
            this.T0002039_Version.Text = u.GetRandomVersion(versionNum_T0002039, "T0002039");
            //this.T0002039_Version.Text = u.UnitType_GetbyTcode("T0002039").TcodeVersion;
            this.T0002040_Version.Text = u.UnitType_GetbyTcode("T0002040").TcodeVersion;
        }
      this.TextBox_T0002038_KccUp.Focus();
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("RRH.aspx");
    }
   
  protected void TextBox_T0002038_KccUp_TextChanged(object sender, EventArgs e)
    {

        T0002038_KccUp_SN = this.TextBox_T0002038_KccUp.Text.Trim();
        if (SNHelper.KccUpSNCheck(T0002038_KccUp_SN,out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002038_KccUp_SN,out message))
                {
                    this.TextBox_T0002038_KccUp.Enabled = false;
                    this.TextBox_T0002038_KccDown.Enabled = true;
                    this.TextBox_T0002038_KccDown.Focus();
                    return;
                }
            }      
       // ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "y", "alert('" +  message + "');", true);
        this.TextBox_T0002038_KccUp.Text = "";
    }

    
    protected void TextBox_T0002038_KccDown_TextChanged(object sender, EventArgs e)
    {
        T0002038_KccDown_SN = this.TextBox_T0002038_KccDown.Text.Trim();

            if (SNHelper.KccDownSNCheck(T0002038_KccDown_SN, out message))
            {
                if (SNHelper.CheckKccDownPec(T0002038_KccDown_SN,kccDownPEC,out message))
                {
                    if (SNHelper.SNCheckKccDownVersion(T0002038_KccDown_SN, kccDownVersion, out message))
                    {                       
                            this.TextBox_T0002038_KccDown.Enabled = false;
                            this.TextBox_T0002038_TAKSN.Enabled = true;
                            this.TextBox_T0002038_TAKSN.Focus();
                            return;
                    }
                }               
            }                           
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002038_KccDown.Text = "";
        this.TextBox_T0002038_KccDown.Focus();
        
    }
    protected void TextBox_T0002038_TAKSN_TextChanged(object sender, EventArgs e)
    {
        T0002038_TAK_SN = this.TextBox_T0002038_TAKSN.Text.Trim();

        if (SNHelper.TCodeSNCheck(T0002038_TAK_SN, out message)) //验证条码长度
        {
            if (SNHelper.SNCheckVersion(T0002038_TAK_SN, "G", out message))//验证版本是否正确
            {
                ainfoanther = a.Assemble_GetbySN(T0002038_TAK_SN);
                if (assembleItem.AssembleItem_GetbyID(ainfoanther.ID).Count != 1)
                {
                    this.TextBox_T0002038_TAKSN.Enabled = false;
                    this.TextBox_T0002039.Enabled = true;
                    this.TextBox_T0002039.Focus();
                    return;
                }
                else
                {
                    message = "T0002038的条码已经存在，无法再次使用！";
                } 
               
            } 
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002038_TAKSN.Text = "";
        this.TextBox_T0002038_TAKSN.Focus(); 
    }
    protected void TextBox_T0002039_TextChanged(object sender, EventArgs e)
    {
        T0002039_SN = this.TextBox_T0002039.Text.Trim();
     
            if (SNHelper.SNCheck(T0002039_SN, "T0002039", out message))
            {
                if (SNHelper.SNCheckVersionRandom(T0002039_SN, this.T0002039_Version.Text, out message))
                {
                    if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002039_SN, "T0002039", out message))
                    {
                        if (ProConditionHelper.CheckSubSNsPrecondition(T0002039_SN, "T0002039", out message))
                        {
                           // if (ProConditionHelper.IsPassOrFail(T0002039_SN, "GetQCStatusBySNPro", out message))
                           
                                this.TextBox_T0002039.Enabled = false;
                                this.TextBox_T0002040.Enabled = true;
                                this.TextBox_T0002040.Focus();
                                return;
                                //this.ConfirmSubmit.Enabled = false;
                                       
                        }
                    }
                }               
            }             
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002039.Text = "";
        this.TextBox_T0002039.Focus();
    }
    protected void TextBox_T0002040_TextChanged(object sender, EventArgs e)
    {
        T0002040_SN = this.TextBox_T0002040.Text.Trim();
        
       if (SNHelper.SNCheck(T0002040_SN, "T0002040", out message))
        {
            if (SNHelper.SNCheckVersion(T0002040_SN, this.T0002040_Version.Text, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002040_SN, "T0002040", out message))
                {
                    if (ProConditionHelper.CheckSubSNsPrecondition(T0002040_SN, "T0002040", out message))
                    {
                        //if (ProConditionHelper.IsPassOrFail(T0002040_SN, "GetQCStatusBySNPro", out message))                    
                            //AssembleInfo ainfo = a.Assemble_GetbySN(T0002040_SN);
                            ainfoanther = a.Assemble_GetbySN(T0002040_SN);
                            if (assembleItem.AssembleItem_GetbyID(ainfoanther.ID).Count == 5)
                            {

                                this.TextBox_T0002040.Enabled = false;
                                this.ConfirmSubmit.Enabled = true;
                                return;
                            }
                            else
                            {
                                message = "请检查T0002040是否装配了除防雷板以外的其他子装配件！";
                            }
                            //this.TextBox_T0002040.Enabled = false;
                            //this.ConfirmSubmit.Enabled = true;
                            //return;       
                      
                    }
                }

            }     
        }
        
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002040.Text = "";
        this.TextBox_T0002040.Focus();
    }
    protected void ConfirmSubmit_Click(object sender, EventArgs e)
    {
        if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002038_KccUp_SN,out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002039_SN, "T0002039", out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002040_SN, "T0002040", out message))
                {
                        Unit u = new Unit();
                        u.Insert(new UnitInfo() { SN = T0002038_TAK_SN, Tcode = "T0002038", IsLeaf = true, IsRoot = true });
                        //  u.Insert(new UnitInfo() { SN = T0002038_KccUp_SN, Tcode = "T0002038", IsLeaf = true, IsRoot = true });
                        // u.Insert(new UnitInfo() { SN = T0002038_KccDown_SN, Tcode = "T0002038", IsLeaf = true, IsRoot = true });
                        u.Insert(new UnitInfo() { SN = T0002039_SN, Tcode = "T0002039", IsLeaf = true, IsRoot = true });
                        u.Insert(new UnitInfo() { SN = T0002040_SN, Tcode = "T0002040", IsLeaf = true, IsRoot = true });
                        KCCSNUnion ku = new KCCSNUnion();
                        KCCSNUnionInfo kui = new KCCSNUnionInfo() { KCCSN1 = T0002038_KccUp_SN, KCCSN2 = T0002038_KccDown_SN, SN = T0002038_TAK_SN, UserID = "111113" };
                        ku.Insert(kui);

                        AssembleInfo ainfo = new AssembleInfo()
                        {
                            ID = 1,
                            SN = T0002038_TAK_SN,
                            IsRoot = true,
                            RecordTime = DateTime.Now,
                            Tcode = "T0002038",
                            Userid = "111113"
                        };
                        dt.Rows.Clear();
                        /*  dr = dt.NewRow();
                         dr["SubSN"] = T0002038_KccUp_SN;
                         dr["Remark"] = "RRH_T0002038_KccUp";
                         dt.Rows.Add(dr);

                         dr = dt.NewRow();
                         dr["SubSN"] = T0002038_KccDown_SN;
                         dr["Remark"] = "RRH_T0002038_KccDown";
                         dt.Rows.Add(dr); */

                        dr = dt.NewRow();
                        dr["SubSN"] = T0002039_SN;
                        dr["Remark"] = "RRH_T0002039RadioFrequencyModule";
                        dt.Rows.Add(dr);

                        dr = dt.NewRow();
                        dr["SubSN"] = T0002040_SN;
                        dr["Remark"] = "RRH_T0002040DigitalModule";
                        dt.Rows.Add(dr);


                        if (a.Assemble_Insert(ainfo, dt))
                        {
                            message = "录入成功";

                            this.TextBox_T0002038_KccUp.Text = "";
                            this.TextBox_T0002038_KccDown.Text = "";
                            this.TextBox_T0002039.Text = "";
                            this.TextBox_T0002040.Text = "";
                            this.TextBox_T0002038_KccUp.Enabled = true;
                            this.TextBox_T0002038_KccUp.Focus();
                            this.ConfirmSubmit.Enabled = false;

                        }
                        else
                        {
                            message = "录入失败，请联系相关人员";
                        }                                                                         
                }
            }
        }                           
       
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }
  
}